/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lt.cj.core.utils;

import java.util.List;
import org.lt.cj.config.entities.ConfigModel;
import org.lt.cj.core.HtmlParserTool;
import org.lt.cj.core.LinkDB;
import org.lt.cj.core.LinkFilter;
import org.lt.cj.core.Seed;
import org.lt.cj.core.filters.LinkFilterImpl;


/*
 * @author luohong
 */
public class ExtractLinksThread implements Runnable {

//    private Set<String> links;
    private List<Seed> links;
    private HtmlParserTool parseTool;
    private LinkFilter filter;
    private String visitingUrl;
    private ConfigModel configModel;
    private LinkDB linkDB;
    private Seed seed;

    public ExtractLinksThread(LinkDB linkDB, ConfigModel configModel, Seed visitingSeed) {

        filter = new LinkFilterImpl(configModel);                //定义一个链接过滤器
        parseTool = new HtmlParserTool(configModel);             //
        this.configModel = configModel;
        this.linkDB = linkDB;
        this.seed = visitingSeed;
    }

    public void run() {
        links = parseTool.fetchSeeds(seed, filter);       //提取出下载网页中的 URL
        //新的未访问的 URL 入队
        if (links != null) {
            for (Seed seed : links) {//将记录插入数据库
                if (seed != null) {
                    linkDB.saveUnvisitedUrl(configModel.getTaskName(), seed);
                }
            }
        }
    }

    public static void main(String[] args) {
//        ExtractLinksThread thread = new ExtractLinksThread("http://www.baidu.com");
    }
}
